/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DisOrder;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DisOrderRepository;
import me.zhengjie.modules.system.service.DisOrderService;
import me.zhengjie.modules.system.service.dto.DisOrderDto;
import me.zhengjie.modules.system.service.dto.DisOrderQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DisOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2023-12-29
**/
@Service
@RequiredArgsConstructor
public class DisOrderServiceImpl implements DisOrderService {

    private final DisOrderRepository disOrderRepository;
    private final DisOrderMapper disOrderMapper;

    @Override
    public Map<String,Object> queryAll(DisOrderQueryCriteria criteria, Pageable pageable){
        Page<DisOrder> page = disOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(disOrderMapper::toDto));
    }

    @Override
    public List<DisOrderDto> queryAll(DisOrderQueryCriteria criteria){
        return disOrderMapper.toDto(disOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DisOrderDto findById(Long orderId) {
        DisOrder disOrder = disOrderRepository.findById(orderId).orElseGet(DisOrder::new);
        ValidationUtil.isNull(disOrder.getOrderId(),"DisOrder","orderId",orderId);
        return disOrderMapper.toDto(disOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DisOrderDto create(DisOrder resources) {
        return disOrderMapper.toDto(disOrderRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DisOrder resources) {
        DisOrder disOrder = disOrderRepository.findById(resources.getOrderId()).orElseGet(DisOrder::new);
        ValidationUtil.isNull( disOrder.getOrderId(),"DisOrder","id",resources.getOrderId());
        disOrder.copy(resources);
        disOrderRepository.save(disOrder);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long orderId : ids) {
            disOrderRepository.deleteById(orderId);
        }
    }

    @Override
    public void download(List<DisOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DisOrderDto disOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单编号", disOrder.getOrderNo());
            map.put("订单类型0：身份，1：课程", disOrder.getOrderType());
            map.put("昵称", disOrder.getNickName());
            map.put("真实姓名", disOrder.getRealName());
            map.put("手机号", disOrder.getMobile());
            map.put("openid", disOrder.getOpenId());
            map.put("上级openid", disOrder.getParentOpenId());
            map.put("订单金额", disOrder.getOrderAmount());
            map.put("佣金", disOrder.getCommission());
            map.put("分佣业务订单号", disOrder.getBizOrderNo());
            map.put("创建时间", disOrder.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}